<?php
// 订单日志
class Order_Log 
{
    public function __construct ($orderId)
    {
        // 无论orderId 是内部ID还是OrderNo, 都必须使用内部ID
        $this->_orderId = Order::getInternalOrderId ($orderId);
    }

    public function add ($logMessage, $time = NULL)
    {
        $log  = utf8::substr($logMessage, 0, 128);
        $time = $time > 0 ? $time : DB::expr('unix_timestamp(now())');

        list($insert_id, $rows) = DB::insert('app_order_log')
            ->columns (array('orderId', 'log', 'created'))
            ->values (array($this->_orderId, $log, $time))
            ->execute();

        return $insert_id > 0;
    }

    public function getLogs ()
    {
        $query = DB::select ()
            ->from('app_order_log')
            ->where('orderId', '=', $this->_orderId)
            ->order_by('created', 'asc')
            ->execute();

        return $query->as_array();
    }
}
